package com.ybbase.framework.controller.system;

import com.ybbase.framework.common.constant.CommonResultCode;
import com.ybbase.framework.model.po.system.Organization;
import com.ybbase.framework.service.system.OrganizationService;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.web.bind.annotation.RestController;
import com.ybbase.framework.base.controller.BaseController;
import com.ybbase.framework.service.system.AreaService;
import com.ybbase.framework.model.po.system.Area;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.List;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParams;
import com.ybbase.framework.base.model.ResponseBean;

/**
 * 系统区域 前端控制器
 *
 * @author cg
 * @since 2019-11-25
 * 云平台2.0
 */
@RestController
@RequestMapping(value = "/area", produces = {"application/json;charset=UTF-8"})
@Api("系统区域")
public class AreaController extends BaseController {

    private static final Logger LOGGER = LogManager.getLogger(AreaController.class);

    @Autowired
    private AreaService targetService;

    @Autowired
    private OrganizationService organizationService;


    /**
     * 系统区域分页查询
     *
     * @param pageIndex 页数
     * @param step      每页数量
     * @return ResponseBean
     * @author cg
     * @date 2019-11-25
     * @since cloud2.0
     */
    @GetMapping("/list")
    @ApiOperation(value = "系统区域分页查询", notes = "系统区域分页查询", response = ResponseBean.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "rows", value = "每页行数", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "area", value = "系统区域", dataType = "Area对象")
    })
    public ResponseBean findListByPage(@RequestParam(name = "page", defaultValue = "1") int pageIndex,
                                       @RequestParam(name = "rows", defaultValue = "20") int step, Area area) {
        try {
            Wrapper<Area> wrapper = new QueryWrapper<Area>();
            Page<Area> page = new Page(0, Integer.MAX_VALUE);
            targetService.page(page, wrapper);
            return new ResponseBean(CommonResultCode.SUCCESS.getCode(),"系统区域分页查询"+CommonResultCode.SUCCESS.getMsg(),page);
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error("系统区域分页查询异常：{}", e.getMessage());
            return new ResponseBean(CommonResultCode.FAIL.getCode(),"系统区域分页查询"+CommonResultCode.FAIL.getMsg());
        }
    }


    /**
     * 系统区域获取全部数据
     *
     * @return ResponseBean
     * @author cg
     * @date 2019-11-25
     * @since cloud2.0
     */
    @GetMapping("/all")
    @ApiOperation(value = "系统区域获取全部数据", notes = "系统区域获取全部数据")
    public ResponseBean findAll() {
        try {
            List<Area> models = targetService.list();
            return new ResponseBean(CommonResultCode.SUCCESS.getCode(),"系统区域获取全部数据"+CommonResultCode.SUCCESS.getMsg(),targetService.findSortArea(models));
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error("系统区域获取全部数据异常：{}", e.getMessage());
            return new ResponseBean(CommonResultCode.FAIL.getCode(),"系统区域获取全部数据"+CommonResultCode.FAIL.getMsg());
        }
    }

    /**
     * 系统区域根据ID查找数据
     *
     * @param id 唯一编码
     * @return ResponseBean
     * @author cg
     * @date 2019-11-25
     * @since cloud2.0
     */
    @GetMapping("/find")
    @ApiOperation(value = "系统区域根据ID查找数据", notes = "系统区域根据ID查找数据")
    @ApiImplicitParam(required = true, name = "id", value = "唯一编码", dataType = "Long")
    public ResponseBean find(@RequestParam("id") Long id) {
        try {
            Area area = targetService.getById(id);
            return new ResponseBean(CommonResultCode.SUCCESS.getCode(),"系统区域根据ID查找数据"+CommonResultCode.SUCCESS.getMsg(),area);
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error("系统区域根据ID查找数据异常：{}", e.getMessage());
            return new ResponseBean(CommonResultCode.FAIL.getCode(),"系统区域根据ID查找数据"+CommonResultCode.FAIL.getMsg());
        }
    }

    /**
     * 系统区域保存数据
     *
     * @param area 系统区域
     * @return ResponseBean
     * @author cg
     * @date 2019-11-25
     * @since cloud2.0
     */
    @PostMapping("/save")
    @ApiOperation(value = "系统区域保存数据", notes = "系统区域保存数据")
    @ApiImplicitParam(required = true, name = "area", value = "系统区域", dataType = "Area对象")
    public ResponseBean save(@RequestBody Area area) {
        try {
            targetService.save(area);
            return new ResponseBean(CommonResultCode.SUCCESS.getCode(),"系统区域保存数据"+CommonResultCode.SUCCESS.getMsg(),area);
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error("系统区域保存数据：{}", e.getMessage());
            return new ResponseBean(CommonResultCode.FAIL.getCode(),"系统区域保存数据"+CommonResultCode.FAIL.getMsg());
        }
    }

    /**
     * 系统区域更新数据
     *
     * @param area 系统区域
     * @return ResponseBean
     * @author cg
     * @date 2019-11-25
     * @since cloud2.0
     */
    @PutMapping("/update")
    @ApiOperation(value = "系统区域更新数据", notes = "系统区域更新数据")
    @ApiImplicitParam(required = true, name = "area", value = "系统区域", dataType = "Area对象")
    public ResponseBean update(@RequestBody Area area) {
        try {
            targetService.updateById(area);
            return new ResponseBean(CommonResultCode.SUCCESS.getCode(),"系统区域更新数据"+CommonResultCode.SUCCESS.getMsg(),area);
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error("系统区域更新数据：{}", e.getMessage());
            return new ResponseBean(CommonResultCode.FAIL.getCode(),"系统区域更新数据"+CommonResultCode.FAIL.getMsg());
        }
    }

    /**
     * 系统区域删除数据
     *
     * @param ids 批量删除数据
     * @return ResponseBean
     * @author cg
     * @date 2019-11-25
     * @since cloud2.0
     */
    @DeleteMapping("/del")
    @ApiOperation(value = "系统区域删除数据", notes = "系统区域删除数据")
    @ApiImplicitParam(required = true, name = "ids", value = "批量删除数据", dataType = "List<Long>")
    public ResponseBean del(@RequestParam("ids") List<Integer> ids) {
        try {
            if (ids != null && !ids.isEmpty()) {
                for (Integer id : ids) {
                    QueryWrapper<Organization> queryWrapper = new QueryWrapper<>();
                    queryWrapper.lambda().eq(Organization::getAreaId, id);
                    List<Organization> list = organizationService.list(queryWrapper);
                    if (list != null && !list.isEmpty()) {
                        return new ResponseBean(CommonResultCode.FAIL.getCode(),"系统区域删除数据"+CommonResultCode.FAIL.getMsg());
                    }
                }
            }
            targetService.removeByIds(ids);
            return new ResponseBean(CommonResultCode.SUCCESS.getCode(),"系统区域删除数据"+CommonResultCode.SUCCESS.getMsg());
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error("系统区域删除数据：{}", e.getMessage());
            return new ResponseBean(CommonResultCode.FAIL.getCode(),"系统区域删除数据"+CommonResultCode.FAIL.getMsg());
        }
    }
}
